//
//  OtherPublishViewController.m
//  cityStar
//
//  Created by Hancong on 15/2/11.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "OtherPublishViewController.h"
#import "RIButtonItem.h"
#import "UIActionSheet+Blocks.h"
#import "UpLoadImageManager.h"
#import "OtherViewController.h"

@interface OtherPublishViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITextFieldDelegate>

@property (nonatomic,strong) NSString *otherType;
@property (nonatomic,assign) int buttonTag;
@property (nonatomic,strong) NSMutableDictionary *dictPhotoIDs;

@end

@implementation OtherPublishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self addBackButton];
    [self addRightButtonWithTitle:@"发布须知" actionBlock:^{
        
    }];
    [self setUpViews];
}

-(void)setUpViews
{
    self.title = @"其他";
    self.scrollerView.contentSize = CGSizeMake(320, 628);
    self.dictPhotoIDs = [[NSMutableDictionary alloc]init];
    self.textDetail.placeholder = @"输入内容";
    self.otherType = @"0";

    self.textDetail.placeholderColor = [UIColor colorWithWhite:0.797 alpha:1.000];
    UIPickerView *typePickerView = [[UIPickerView alloc]init];
    typePickerView.delegate = self;
    typePickerView.tag = 0;
    self.fieldType.inputView = typePickerView;
    
    UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    [topView setBarStyle:UIBarStyleDefault];
    
    UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];
    
    NSArray * buttonsArray = [NSArray arrayWithObjects:btnSpace,doneButton,nil];
    
    [topView setItems:buttonsArray];
    [self.fieldType setInputAccessoryView:topView];

}

-(void)dismissKeyBoard
{
    [self.fieldType resignFirstResponder];
}

#pragma mark - pickerViewDelegate
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
        return 3;
}

-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
        if (row == 0) {
            return @"咨询";
        }else if (row == 1){
            return @"常识";
        }else
        {
            return @"问答";
        }
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
        if (row == 0) {
            self.fieldType.text = @"咨询";
            self.otherType = @"0";
        }else if (row == 1){
            self.fieldType.text = @"常识";
            self.otherType = @"1";
        }else
        {
            self.fieldType.text = @"问答";
            self.otherType = @"2";
        }
}

#pragma mark - Actions
-(IBAction)btnPublish_Touch:(id)sender
{
    NSString *errMsg;
    if (self.fieldTitle.text.length == 0) {
        errMsg = @"请输入标题";
    }else if (self.fieldType.text.length == 0){
        errMsg = @"请选择类型";
    }else if (self.fieldName.text.length == 0){
        errMsg = @"请输入联系人";
    }else if (self.fieldPhone.text.length == 0){
        errMsg = @"请输入联系电话";
    }else if (self.textDetail.text.length == 0){
        errMsg = @"请输入详情描述";
    }
    if (errMsg) {
        BossSystemShowAlertViewWithTitle(errMsg)
        return;
    }
    NSString *cityID = [[NSUserDefaults standardUserDefaults]objectForKey:@"city_code"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    NSDictionary *parameters = @{@"title":self.fieldTitle.text,
                                 @"style":self.otherType,
                                 @"contact":self.fieldName.text,
                                 @"phone":self.fieldPhone.text,
                                 @"description":self.textDetail.text,
                                 @"city":cityID?cityID:@"122",
                                 };
    [self.dictPhotoIDs addEntriesFromDictionary:parameters];

    [HttpRequestManager postMethod:KInterface_PublishOther parameters:self.dictPhotoIDs completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        DLog(@"%@",response);
        if (success) {
            [SVProgressHUD showSuccessWithStatus:@"发布成功"];
            OtherViewController *otherVC = [[OtherViewController alloc]init];
            [self.navigationController pushViewController:otherVC animated:YES];
        }else{
            [SVProgressHUD showSuccessWithStatus:errMsg];
        }
    }];
}

-(IBAction)getPhoto_Touch:(UIButton *)sender
{
    [self.view endEditing:YES];
    self.buttonTag = (int)sender.tag;
    __block UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    
    RIButtonItem *takePhotoButton = [RIButtonItem itemWithLabel:@"拍照" action:^{
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:imagePickerController animated:YES completion:nil];
    }];
    RIButtonItem *choosePhotoButton = [RIButtonItem itemWithLabel:@"从相册选取" action:^{
        imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:imagePickerController animated:YES completion:nil];
    }];
    
    UIActionSheet *imageSourcePickActionSheet;
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        imageSourcePickActionSheet = [[UIActionSheet alloc] initWithTitle:@"添加照片" cancelButtonItem:nil destructiveButtonItem:[RIButtonItem itemWithLabel:@"取消"] otherButtonItems:takePhotoButton,choosePhotoButton,nil];
    }else{
        imageSourcePickActionSheet = [[UIActionSheet alloc] initWithTitle:@"添加照片" cancelButtonItem:nil destructiveButtonItem:[RIButtonItem itemWithLabel:@"取消"] otherButtonItems:choosePhotoButton,nil];
    }
    [imageSourcePickActionSheet showInView:self.view];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [picker dismissViewControllerAnimated:YES completion:^{
        switch (self.buttonTag) {
            case 1:
            {
                [self.btnAddImage01 setImage:editingInfo[UIImagePickerControllerOriginalImage] forState:UIControlStateNormal];
                [self.btnAddImage01 setTitle:@"" forState:UIControlStateNormal];
                [self.btnAddImage01 setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
                [UpLoadImageManager uploadImage:editingInfo[UIImagePickerControllerOriginalImage] withCompltedBlock:^(BOOL success, NSString *urlString) {
                    if (success) {
                        [self.dictPhotoIDs setObject:urlString forKey:@"image[0]"];
                        DLog(@"%@",urlString);
                    }
                }];
            }
                
                break;
            case 2:
            {
                [self.btnAddImage02 setImage:editingInfo[UIImagePickerControllerOriginalImage] forState:UIControlStateNormal];
                [self.btnAddImage02 setTitle:@"" forState:UIControlStateNormal];
                [self.btnAddImage02 setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

                [UpLoadImageManager uploadImage:editingInfo[UIImagePickerControllerOriginalImage] withCompltedBlock:^(BOOL success, NSString *urlString) {
                    if (success) {
                        [self.dictPhotoIDs setObject:urlString forKey:@"image[1]"];
                        DLog(@"%@",urlString);
                    }
                }];
            }
                break;
            case 3:
            {
                [self.btnAddImage03 setImage:editingInfo[UIImagePickerControllerOriginalImage] forState:UIControlStateNormal];
                [self.btnAddImage03 setTitle:@"" forState:UIControlStateNormal];
                [self.btnAddImage03 setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

                [UpLoadImageManager uploadImage:editingInfo[UIImagePickerControllerOriginalImage] withCompltedBlock:^(BOOL success, NSString *urlString) {
                    if (success) {
                        [self.dictPhotoIDs setObject:urlString forKey:@"image[2]"];
                        DLog(@"%@",urlString);
                    }
                }];
            }
                break;
            case 4:
            {
                [self.btnAddImage04 setImage:editingInfo[UIImagePickerControllerOriginalImage] forState:UIControlStateNormal];
                [self.btnAddImage04 setTitle:@"" forState:UIControlStateNormal];
                [self.btnAddImage04 setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

                [UpLoadImageManager uploadImage:editingInfo[UIImagePickerControllerOriginalImage] withCompltedBlock:^(BOOL success, NSString *urlString) {
                    if (success) {
                        [self.dictPhotoIDs setObject:urlString forKey:@"image[3]"];
                        DLog(@"%@",urlString);
                    }
                }];
            }
                break;
            case 5:
            {
                [self.btnAddImage05 setImage:editingInfo[UIImagePickerControllerOriginalImage] forState:UIControlStateNormal];
                [self.btnAddImage05 setTitle:@"" forState:UIControlStateNormal];
                [self.btnAddImage05 setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

                [UpLoadImageManager uploadImage:editingInfo[UIImagePickerControllerOriginalImage] withCompltedBlock:^(BOOL success, NSString *urlString) {
                    if (success) {
                        [self.dictPhotoIDs setObject:urlString forKey:@"image[4]"];
                        DLog(@"%@",urlString);
                    }
                }];
            }
                break;
                
            default:
                break;
        }
    }];
}

#pragma mark - textFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    switch (textField.tag) {
        case 2:
        {
            [UIView animateWithDuration:0.3f animations:^{
                //                self.scrollerView.contentOffset = CGPointMake(0, 50);
                textField.text = @"咨询";
            }];
        }
            break;
            
        default:
            break;
    }
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
